/**
 * 
 */
package com.shy.base.util;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author F3219057 劉曉橋
 * @date 2017年6月10日
 */
public class IpUtil {

	/**
     * log
     */
	private static Logger logger = LogManager.getLogger(IpUtil.class);
    
    /**
     * 
     * @param request
     *            可以获得反向及多级反向代理后的前端IP
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        try {
            String ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0
                    || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")
                        || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    if (inet != null){
                        ipAddress = inet.getHostAddress();
                    }
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                                                                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
            return ipAddress;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("client ip get error: " + e);
        }
        return "0.0.0.0";
    }

    public static String getMACAddress(String ip) throws Exception{
        String str = "";
        String macAddress = "";
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            while ((str = input.readLine()) != null) {
                if (str.indexOf("MAC") > 1) {
                    // 使用substring函数截出mac地址
                    // macAddress = str.substring(str.indexOf("MAC") + 9,
                    // str.length());
                    break;
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return macAddress;
    }

    public static String getLocalIP() throws Exception{
        InetAddress addr = null;
        String ip = "";
        try {
            addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress().toString();// 获得本机IP
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }
        return ip;
        // address=addr.getHostName()toString;//获得本机名称
    }
}
